﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Data;
using System.Data.SqlClient;

namespace capaNegocios
{
    public class clsCurso
    {
        public String id { get; set; }
        public String nombre { get; set; }
        public int horasTeoricas { get; set; }
        public int horasPracticas { get; set; }
        public String descripcion { get; set; }
        public char estado { get; set; }

        clsConexion oClsConexion = new clsConexion();
        SqlConnection objConexion = new SqlConnection();

        public bool Agregar()
        {
            clsConexion oClsConexion = new clsConexion();
            SqlConnection objConexion = new SqlConnection();
            SqlCommand objComando = new SqlCommand();
            
            try
            {
                objConexion = oClsConexion.AbrirConexion();

                objComando.CommandType = CommandType.StoredProcedure;
                objComando.CommandText = "USP_I_AGREGARCURSO";

                objComando.Parameters.Add(new SqlParameter("@NOMBRES", SqlDbType.VarChar));
                objComando.Parameters.Add(new SqlParameter("@DESCRIPCION", SqlDbType.VarChar));
                objComando.Parameters.Add(new SqlParameter("@HORATECNICA", SqlDbType.VarChar));
                objComando.Parameters.Add(new SqlParameter("@HORAPRACTICA", SqlDbType.VarChar));
                
                objComando.Parameters["@NOMBRES"].Value = this.nombre;
                objComando.Parameters["@DESCRIPCION"].Value = this.descripcion;
                objComando.Parameters["@HORATECNICA"].Value = this.horasTeoricas;
                objComando.Parameters["@HORAPRACTICA"].Value = this.horasPracticas;
                
                objComando.Connection = objConexion;
                objComando.ExecuteNonQuery();

                return true;
            }
            catch (Exception ex)
            {
                return false;
                throw ex;
            }
            finally
            {
                objConexion.Close();
            }            
        }

        public bool Actualizar()
        {
            clsConexion oClsConexion = new clsConexion();
            SqlConnection objConexion = new SqlConnection();
            SqlCommand objComando = new SqlCommand();
            
            try
            {
                objConexion = oClsConexion.AbrirConexion();

                objComando.CommandType = CommandType.StoredProcedure;
                objComando.CommandText = "USP_U_MODIFICARCURSO";

                objComando.Parameters.Add(new SqlParameter("@ID", SqlDbType.VarChar));
                objComando.Parameters.Add(new SqlParameter("@NOMBRES", SqlDbType.VarChar));
                objComando.Parameters.Add(new SqlParameter("@DESCRIPCION", SqlDbType.VarChar));
                objComando.Parameters.Add(new SqlParameter("@HORATECNICA", SqlDbType.VarChar));
                objComando.Parameters.Add(new SqlParameter("@HORAPRACTICA", SqlDbType.VarChar));
                objComando.Parameters.Add(new SqlParameter("@ESTADO", SqlDbType.Char));

                objComando.Parameters["@ID"].Value = this.id;
                objComando.Parameters["@NOMBRES"].Value = this.nombre;
                objComando.Parameters["@DESCRIPCION"].Value = this.descripcion;
                objComando.Parameters["@HORATECNICA"].Value = this.horasTeoricas;
                objComando.Parameters["@HORAPRACTICA"].Value = this.horasPracticas;
                objComando.Parameters["@ESTADO"].Value = this.estado;
                
                objComando.Connection = objConexion;
                objComando.ExecuteNonQuery();

                return true;
            }
            catch (Exception ex)
            {
                return false;
                throw ex;
            }
            finally
            {
                objConexion.Close();
            }
        }

        public DataTable Buscar()
        {
            try
            {
                SqlCommand objComando = new SqlCommand();
                SqlDataAdapter objDataAdapter = new SqlDataAdapter();
                DataTable objDataTable = new DataTable();

                objConexion = oClsConexion.AbrirConexion();

                objComando.CommandType = CommandType.StoredProcedure;
                objComando.CommandText = "USP_S_BUSCARCURSOS";

                objComando.Connection = objConexion;
                objComando.ExecuteNonQuery();

                objDataAdapter.SelectCommand = objComando;
                objDataAdapter.Fill(objDataTable);                        

                return objDataTable;
            }
            catch (SqlException ex)
            {
                return null;
                throw ex;
            }
            finally
            {
                objConexion.Close();
            }
        }

        public DataTable BuscarPorCodigo()
        {
            try
            {
                SqlCommand objComando = new SqlCommand();
                SqlDataAdapter objDataAdapter = new SqlDataAdapter();
                DataTable objDataTable = new DataTable();

                objConexion = oClsConexion.AbrirConexion();

                objComando.CommandType = CommandType.StoredProcedure;
                objComando.CommandText = "USP_S_BUSCARCURSOSCODIGO";

                objComando.Parameters.Add(new SqlParameter("@ID", SqlDbType.VarChar));
                objComando.Parameters["@ID"].Value = this.id;

                objComando.Connection = objConexion;
                objComando.ExecuteNonQuery();

                objDataAdapter.SelectCommand = objComando;
                objDataAdapter.Fill(objDataTable);

                return objDataTable;
            }
            catch (SqlException ex)
            {
                return null;
                throw ex;
            }
            finally
            {
                objConexion.Close();
            }
        }
    }
}
